package com.zuxia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zuxia.entity.JwtInfo;
import com.zuxia.entity.Member;
import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.mapper.MemberMapper;
import com.zuxia.param.UserParam;
import com.zuxia.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuxia.service.RoleMemberService;
import com.zuxia.utils.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author laoli
 * @since 2024-11-17
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private RoleMemberService roleMemberService;
    @Resource
    private RedisUtils redisUtil;
    @Resource
    private RedisTokenUtil redisTokenUtil;
    @Resource
    private MSMUtil msmUtil;

    @Override
    public String LoginPwd(UserParam userParam) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getMobile,userParam.getUserPhone());
        Member user = memberMapper.selectOne(wrapper);
        if(user==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.USER_NOT_FOUND);
        }
        if(!user.getPassword().equals(userParam.getUserPwd())){
            throw new ZuxiaBusinessException(ResultCodeEnum.USER_PASS_ERR0R);
        }
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(user.getId());
        jwtInfo.setNickname(user.getNickname());
        jwtInfo.setAvatar(user.getAvatar());
        int roleId = roleMemberService.getRoleId(user.getId());
        jwtInfo.setRoleId(roleId);
        String accessToken = JwtUtils.getJwtToken(jwtInfo, 24*60*60 * 1000L);
        redisTokenUtil.setToken(jwtInfo,accessToken,7*24*60*60*1000L);
        return accessToken;
    }

    @Override
    public String LoginPhone(String phone, String yzm) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getMobile,phone);
        Member user = memberMapper.selectOne(wrapper);
        if(user==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.USER_NOT_FOUND);
        }
        String redisCode = redisUtil.getRedisCode(phone);
        if("".equals(redisCode)||redisCode==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.YZM_IS_NULL);
        }else if(!redisCode.equals(yzm)){
            throw new ZuxiaBusinessException(ResultCodeEnum.YZM_IS_FALSS);
        }
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(user.getId());
        jwtInfo.setNickname(user.getNickname());
        jwtInfo.setAvatar(user.getAvatar());
        int roleId = roleMemberService.getRoleId(user.getId());
        jwtInfo.setRoleId(roleId);
        String accessToken = JwtUtils.getJwtToken(jwtInfo, 1 * 10 * 1000L);
        redisTokenUtil.setToken(jwtInfo,accessToken,7*24*60*60*1000L);
        return accessToken;
    }

    @Override
    public String sendYZM(String phone) {
        Boolean send;
        try {
            redisUtil.limitRedisKey(phone);
            String aLong = redisUtil.getLimitRedisKey(phone);
            System.out.println(phone+"当天访问次数："+aLong);
            if (Long.parseLong(aLong)>20){
                throw new ZuxiaBusinessException(ResultCodeEnum.YZM_TIME_OLD);
            }
            send = redisUtil.SendingRedisCode(phone, msmUtil.Test(phone), 1L);
        }catch (Exception e){
            throw e;
        }
        return "type:"+send+","+"time:"+ Instant.now().toString();
    }
}
